package com.yn.xr.common.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtils {
    /**
     * �?��字符串是否为空白 empty ("") or null
     * 建议使用 org.apache.commons.lang3.StringUtils.isBlank()
     * @param str
     * @return
     */
	public static boolean isEmpty(String str) {
		return str==null || str.trim().equals("");
	}
	/**
	 * 
	 * @function:去掉�?��空格  
	 * @param str
	 * @return String   
	 * @exception 
	 * @author:肖震   
	 * @since  1.0.0
	 */
	public static String trimAllEmpty(String str)
	{
		return str==null?str:str.replaceAll(" +","");
	}
	
	/**   
	 * @function:  生成18位流水号，格式如：20141215223209678776
	 * @return String   
	 * @exception 
	 * @author:Wuwenyong  
	 * @since  1.0.0   
	*/
	public static String getBusinessNum()
	{
		Date date = new Date();
		String sn = DateUtil.dateToString(date, "yyyyMMdd");
		String cur = DateUtil.dateToString(date, "yyyyMMddHHmmss");
		String sn_business_num1 = System.getProperty("sn_business_num");
		if(sn_business_num1 == null || "".equals(sn_business_num1))
		{
			System.setProperty("sn_business_num", sn);//设置已标识时间串
			System.setProperty("business_num_key", "1");//设置自增长标识，从1开始
			return cur+"0001";
		}
		else
		{
			int startNum = 1;//尾部流水从1开始
			String sn_business_num = sn;//当天标识
			String key = "";
			if(sn_business_num.equals(System.getProperty("sn_business_num")))
			{
				key = System.getProperty("business_num_key");
				if(key != null && !"".equals(key))
				{
					startNum = Integer.parseInt(key);
					startNum++;
				}
			}
			
			String endNum = ""+startNum;
			if(endNum.length()==1)//自动标识长度为1
			{
				key = "000"+startNum;
			}
			else if(endNum.length()==2)//自动标识长度为2
			{
				key = "00"+startNum;
			}
			else if(endNum.length()==3)//自动标识长度为3
			{
				key = "0"+startNum;
			}
			System.setProperty("sn_business_num", sn);
			System.setProperty("business_num_key", ""+startNum);
			return cur+key;
		}
	}
	
	/**   
	 * @function:  随机8位数字邀请码
	 * @return int   
	 * @exception 
	 * @author:Wuwenyong  
	 * @since  1.0.0   
	*/
	public static int inviteCode(){
		int code = 0;
		int number = 100000000;
		while (true)
		{
			code = (int) (Math.random() * number);
			if (String.valueOf(code).length() == 8 && code > 10000000
					&& code < 99999999)
			{
				break;
			}
		}
		return code;
	}
	/**
	 * 
	 * @function: 将空字符串转为null 
	 * @param str 空字符串
	 * @return 
	 * @return type Object   
	 * @exception 
	 * @author:肖震   
	 * @since  1.0.0
	 */
	public static Object covertEmptyStringToNull(String str)
	{
		if(str!=null&&"".equals(str.trim()))
		{
			return null;
		}else
		{
			return str;
		}
	}
	/**
	 * 
	 * @function: 将null转为空字符串 
	 * @param str
	 * @return 
	 * @return type Object   
	 * @exception 
	 * @author:肖震   
	 * @since  1.0.0
	 */
	public static String covertNullToEmptyString(String str)
	{
		if(str==null)
		{
			return "";
		}else
		{
			return str;
		}
	}
	public static Object covertNullObjectToEmptyString(Object str)
	{
		if(str==null)
		{
			return "";
		}else
		{
			return str;
		}
	}
	
	//金额保存两位小数，并且不四舍五入
	public static BigDecimal moneyToKeepDecimals(Object object,int len)
	{
		String newMoney = "0.00";
		if(null != object)
		{
			String money = object.toString();
			if(money.contains("."))
			{
				String[] strArray = money.split("\\.");
				if(1 == strArray[1].length())
				{
					newMoney = money + "0";
				}
				else if(2 == strArray[1].length())
				{
					newMoney = money;
				}
				else
				{
					newMoney = strArray[0]+"."+strArray[1].substring(0,len);
				}
			}
			else
			{
				newMoney = money + ".00";
			}
		}
		return new BigDecimal(newMoney);
	}
	
	public static boolean isNumeric(String str){
		  for (int i = 0; i < str.length(); i++){
		   System.out.println(str.charAt(i));
		   if (!Character.isDigit(str.charAt(i))){
		    return false;
		   }
		  }
		  return true;
		 }
	
	
	public static boolean isNumericPointFour(String str)
	{
		Pattern pattern = Pattern
				.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,4})?$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches())
		{
			return false;
		}
		return true;
	}
	
	/**
     * 校验所有字符串参数是否为空
     * @createTime:2015年3月26日
     * @param strs 待校验字符串
     * @return      true:有参数为空、
     * @author:汪品文
     * @version:1.0.0
     */
    public static Boolean isEmptys( String ...strs ){
        for( String parm : strs ){
            if( StringUtils.isEmpty(parm) ){
                return true;
            }
        }
        return false;
    }
    
    /**
     * 验证是不是数字(没有小数点)
     * (不做空校验)
     * @param number 
     * @return 是：true
     */
    public static boolean isInteger(String number){
       String CNUMBER_PATTERN = "^[0-9]*$";// 判断数字的正则表达式
       Pattern p = Pattern.compile(CNUMBER_PATTERN);
       Matcher m = p.matcher(number);
       return m.find();
    }
    
    /**
     * 
     * @function: 验证 传过来的金额 是否正确 （金额大于0且最多支持4位小数）
     * @param money
     * @return boolean
     * @exception
     * @author:孙元宝
     * @since 1.0.0
     */
    public static boolean CheckMoney(String money)
    {
        if (money != null && Double.parseDouble(money) > 0)
        {
            return true;
        }
        return false;
    }
    
    /**
     * java 校验惠信号 格式是否正确
     * @description		必须已字符开头，只能是字符+数字或下划线，长度只能是5-19位
     * @fileName:StringUtils.java
     * @createTime:2015年6月3日
     * @param accName
     * @return
     * @author:yangjun
     * @version:1.0.0
     */
    public static boolean CheckAccName(String accName){
    	Pattern pattern = Pattern.compile("^[A-Za-z]{1}+([A-Za-z0-9]|[_]){4,18}$");
        Matcher matcher = pattern.matcher(accName);
        return matcher.matches();
    }
    
    /**
     * java 验证手机号是否正确
     * @description	
     * @fileName:StringUtils.java
     * @createTime:2015年6月4日
     * @param phone
     * @return
     * @author:yangjun
     * @version:1.0.0
     */
    public static boolean checkMobile(String mobile){
        Pattern pattern = Pattern.compile("^((1[3-9][0-9]))\\d{8}$"); 
        Matcher matcher = pattern.matcher(mobile);       
        return matcher.matches();
    }
}
